package UI
{
	//import eDpLib.events.ProxyEvent;
	import flash.display.Bitmap;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import com.greensock.TweenMax;
	
	import ButtonFunctions;
	
	/**
	 * ...
	 * @author Bryan Keiren & Sander van Katwijk
	 */
	public class Button extends Sprite
	{		
		[Embed(source = "/../lib/btn_over_type0.png")] public static const BtnImageOver_Type0:Class;
		[Embed(source = "/../lib/btn_down_type0.png")] public static const BtnImageDown_Type0:Class;
		[Embed(source = "/../lib/btn_up_type0.png")] public static const BtnImageUp_Type0:Class;
		[Embed(source = "/../lib/btn_inactive_type0.png")] public static const BtnImageInactive_Type0:Class;
	
		[Embed(source = "/../lib/btn_over_type1.png")] public static const BtnImageOver_Type1:Class;
		[Embed(source = "/../lib/btn_down_type1.png")] public static const BtnImageDown_Type1:Class;
		[Embed(source = "/../lib/btn_up_type1.png")] public static const BtnImageUp_Type1:Class;
		[Embed(source = "/../lib/btn_inactive_type1.png")] public static const BtnImageInactive_Type1:Class;
		
		public var text:TextField = new TextField();
		public var data;	// Custom data that is passed to the mouse functions.
		
		private function onMouseUp(evt:MouseEvent):void
		{
			if (m_Active)
			{
				getChildAt(0).visible = true;
				getChildAt(1).visible = false;
				getChildAt(2).visible = false;
				
				if (m_onMouseUp != null)
				{
					m_onMouseUp(data);
				}
			}
		}
		
		private function onMouseDown(evt:MouseEvent):void
		{
			if (m_Active)
			{
				getChildAt(0).visible = false;
				getChildAt(1).visible = false;
				getChildAt(2).visible = true;
				
				if (m_onMouseDown != null)
				{
					m_onMouseDown(data);
				}
			}
		}
		
		private function onMouseOver(evt:MouseEvent):void
		{
			if (m_Active)
			{
				getChildAt(0).visible = false;
				getChildAt(1).visible = true;
				getChildAt(2).visible = false;
				
				if (m_onMouseOver != null)
				{
					m_onMouseOver(data);
				}
				
				if (!(TweenMax.isTweening(this)))
				{
					TweenMax.to(this, 0.05, { x:(this.start_x + 15 - (this.x - this.start_x)) } );
					TweenMax.to(text, 0.05, { x:(this.start_x + 15 - (this.x - this.start_x)) } );
				}
			}
		}
		
		private function onMouseOut(evt:MouseEvent):void
		{
			if (m_Active)
			{
				getChildAt(0).visible = true;
				getChildAt(1).visible = false;
				getChildAt(2).visible = false;
				
				if (m_onMouseOut != null)
				{
					m_onMouseOut(data);
				}
				
				if (!(TweenMax.isTweening(this)))
				{
					TweenMax.to(this, 0.05, { x:(this.x - (this.x - this.start_x)) } );
					TweenMax.to(text, 0.05, { x:(this.x - (this.x - this.start_x)) } );
				}
			}
		}
		
		private function onMouseDoubleClick(evt:MouseEvent):void
		{
			if (m_Active)
			{
				if (m_onMouseDoubleClick != null)
				{
					m_onMouseDoubleClick(data);
				}
			}
		}
		
		private var m_onMouseUp:Function = null;
		private var m_onMouseDown:Function = null;
		private var m_onMouseOver:Function = null;
		private var m_onMouseOut:Function = null;
		private var m_onMouseDoubleClick:Function = null;
		
		private var m_Active:Boolean = true;
		private var m_Name:String;
		
		private var start_x:Number = x;
		private var start_y:Number = y;
		
		// NOTE: This c-tor does NOT get coordinates and dimensions passed because it won't work that way. The sprite will NOT show. Ever.
		// Therefore, after instantiating a button, it's members must be set by using the set functions. Do NOT access the members directly, because
		// the button's textfield must also be set.
		// a_ImageType represents which type of image is used for the button. There are a only few valid values for this. Check the implementation
		// of this function to see the exact range. The goal of this system is to provide a quick way to enable the different kinds of backgrounds
		// that are needed for regular buttons and for stretched buttons (in the game lobby).
		public function Button(a_Name:String, a_FunctionMouseUp:Function = null, a_FunctionMouseDown:Function = null, a_FunctionMouseOver:Function = null, a_FunctionMouseOut:Function = null, a_FunctionMouseDoubleClick:Function = null, a_ImageType:int = 0)
		{
			m_Name = a_Name;
			
			switch (a_ImageType)
			{
				case 1:
				{
					addChild( new BtnImageUp_Type1 as Bitmap );
					addChild( new BtnImageOver_Type1 as Bitmap );
					addChild( new BtnImageDown_Type1 as Bitmap );
					addChild( new BtnImageInactive_Type1 as Bitmap );
					break;
				}
				case 0:
				default:
				{
					addChild( new BtnImageUp_Type0 as Bitmap );
					addChild( new BtnImageOver_Type0 as Bitmap );
					addChild( new BtnImageDown_Type0 as Bitmap );
					addChild( new BtnImageInactive_Type0 as Bitmap );
					break;
				}
			}
			
			
			Engine.Get().addChild(this);
			Engine.Get().addChild(text);
			
			getChildAt(0).visible = true;
			getChildAt(1).visible = false;
			getChildAt(2).visible = false;
			getChildAt(3).visible = false;
			
			m_onMouseDown = a_FunctionMouseDown;
			m_onMouseUp = a_FunctionMouseUp;
			m_onMouseOut = a_FunctionMouseOut;
			m_onMouseOver = a_FunctionMouseOver;
			m_onMouseDoubleClick = a_FunctionMouseDoubleClick;
			
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			text.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			text.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			text.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			text.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.DOUBLE_CLICK, onMouseDoubleClick);
			text.addEventListener(MouseEvent.DOUBLE_CLICK, onMouseDoubleClick);
			
			
			text.wordWrap = true;
			text.selectable = false;
			text.textColor = 0xffffff;
			buttonMode = true;
			
			enable();
		}
		
		public function getName():String
		{
			return m_Name;
		}
		
		public function deactivate():void
		{
			m_Active = false;
			getChildAt(3).visible = true;
		}
		
		public function activate():void
		{
			m_Active = true;
			getChildAt(3).visible = false;
		}
		
		public function disable():void
		{
			this.visible = false;
			text.visible = false;
			//Engine.printf("Disabling " + text.text);
		}
		
		public function enable():void
		{
			this.visible = true;
			text.visible = true;
			//Engine.printf("Enabling " + text.text);
		}
		
		public function setX(a_X:Number):void
		{
			x = a_X;
			text.x = a_X;
			start_x = x;
		}
		
		public function setY(a_Y:Number):void
		{
			y = a_Y;
			text.y = a_Y;
			start_y = y;
		}
		
		public function setWidth(a_Width:Number):void
		{
			width = a_Width;
			text.width = a_Width;
		}
		
		public function setHeight(a_Height:Number):void
		{
			height = a_Height;
			text.height = a_Height;
		}
	}

}